package dmt16.TelemetrySuit.Utils
{
	public final class Calculation
	{
		public function Calculation()
		{
			throw new Error("Calculation is a static class and does not have a constructor");
		}
		
		public static function mean(data:Vector.<Number>):Number{
			var result:Number=0;
			if(data.length==0)
				return 0;
			for(var i:Number=0; i<data.length; i++){
				result+=data[i];
			}
			return result/data.length;
		}
		
		public static function movingAverage(data:Vector.<Number>,windowSize:Number):Vector.<Number>{
			var result:Vector.<Number>=new Vector.<Number>();
			if(windowSize>data.length){
				result.push(mean(data));
				return result;
			}
			var x:Number=0;
			for(var j:Number=0; j<windowSize;j++){
				x+=data[j];
			}
			result.push(x/windowSize);
			for(var i:Number=0; i<data.length-windowSize-1;i++){
				x+=data[i+windowSize]-data[i];
				result.push(x/windowSize);
			}
			return result;
		}
		
		public static function max(data:Vector.<Number>):Number{
			if(data&&data.length!=0){
				var result:Number=data[0];
				for(var i:Number=1;i<data.length;i++){
					result=(data[i]>result)?data[i]:result;
				}
				return result;
			}
			else
				return 0;
		}
		
		public static function min(data:Vector.<Number>):Number{
			if(data&&data.length!=0){
				var result:Number=data[0];
				for(var i:Number=1;i<data.length;i++){
					result=(data[i]<result)?data[i]:result;
				}
				return result;
			}
			else
				return 0;
		}
	}
}